<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use brand\Brand;
use brand\build\Columns;
use brand\logic\BrandHelper;
use brand\logic\Config;
use brand\page\Form;
use brand\page\Table;
use brand\tools\Functions;
use think\facade\Db;


/**
 * 配置信息
 */

class ConfigValue extends Base
{
    public $model;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ConfigValue();
    }


    //CURD文件名，默认为数据表名，请不要任意修改！
    public $curdName = 'config_value';


    public function table_init()
    {
        Table::$add_url = url('add',request()->get())->build();
    }



    public function value()
    {
        if ($this->request->isAjax()){
            $params = $this->request->param();
            $raw = $this->model->find($params['id']);
            if (!isset($params['value'])) return _error('参数错误');
            if (is_array($params['value'])) $params['value'] = json_encode($params['value']);
            $raw->save($params);
            return _success('操作成功');
        }


        $id = $this->request->get('id');
        $value = $this->model->find($id);
        if (!$value) $this->error('数据不存在');

        $columns_param = json_decode($value['column'],1) ?? [];
        if (!$columns_param) $this->error('配置不存在');

        $config = [];
        $ClassName = Config::BRAND_NAMESPACE . Functions::toCamelCase($value['column_method']);

        $form['method'] = $value['column_method'];
        $table =[];
        if (class_exists($ClassName)){
            $ClassObject = new $ClassName();
            $params['param'] = $columns_param;
            [$form,$table] = $ClassObject->methodFormConf($form,$table,$params);

        }
        $config['form'] = $form;
        $config['title'] = $value['value_title'];

        $form = $this->form->form();
        $columns = $this->form->buildForm($form,['value'=>$config],$value);
        $form->columns($columns);
        $form->options(
            [
                'async' => [
                    'url' => "",
                ],
            ]
        );
        return $form->view();

    }


    public function form_before_build($raw)
    {

        $columns = Columns::getConfig();

        //类型
        $brandList = Config::getBrandList();
        $brandOptions = BrandHelper::formatOptions($brandList);

        $columns['column_method'] = [
            'form'  =>  [
                'method'    =>  'select',
                'options' =>$brandOptions,
            ],
            'title' =>  '类型'
        ];

        $raw['column_method'] = $raw['column_method'] ?? 'Input';

        //循环支持的所有表单
        foreach ($brandList as $brandMethod => $brandLabel) {
            $ClassName = Config::BRAND_NAMESPACE . Functions::toCamelCase($brandMethod);
            if (class_exists($ClassName)){

                $ClassObject = new $ClassName();
                $methodConfig = $ClassObject->buildConfigColumn([],'column_method');

                $columns[$brandMethod] = $methodConfig['column'];
                $raw[$brandMethod] = $raw['column'] ?? $methodConfig['value'];

            }
        }
        Form::setAsyncUrl (url('submitFromConf',request()->get())->build());
        Columns::setConfig($columns);
        return $raw;
    }

    public function submitFromConf()
    {
        $config_id = $this->request->get('config_id');
        $post = $this->request->post();

        $data['value_title'] = $post['value_title'] ?? "";
        if($config_id) $data['config_id'] = $config_id;
        $data['label'] = $post['label'] ?? "";
        $data['weigh'] = $post['weigh'] ?? 0;
        if (isset($post['value']) && $post['value']) $data['value'] = $post['value'];
        $data['column_method'] = $post['column_method'] ?? "";
        $data['column'] = json_encode($post[$data['column_method']] ?? []);


        if (isset($post['id']) && $post['id']){
            $this->model->where('id',$post['id'])->update($data);
        }else{
            $this->model->save($data);
        }

        return _success('操作成功');
    }



}